# coding: utf8


import os
import logging
import time
import re
import traceback
import requests_html
from multiprocessing.pool import ThreadPool


"""同济大学
电子科学与技术系
"""

logging.basicConfig(level=logging.INFO)

session = requests_html.HTMLSession()
pool = ThreadPool(os.cpu_count() * 2)  # 要使用 hanlp, 线程数设为1


sex_ptn = re.compile(r'([男|女])')
degree_ptn = re.compile(r'学位[:：](\S+)')
dep_ptn = re.compile(r'所在单位[:：](\S+)')
subj_ptn = re.compile(r'学科[:：]([ \S]+)')

addr_ptn = re.compile(r'(?:通信地址|通讯地址|办公地址|办公地点|Address)[:：]([ \S]+)')
tel_ptn = re.compile(r'(?:办公电话|电话|联系方式|Tel|tel)[:：]([0-9+\- ,]+)')
email_ptn = re.compile(r'(?:电子邮箱|电子信箱|E-mail|Email|email|e-mail|邮箱地址|邮\s*箱)[:：](\S+@[.\S]+)')
ri_ptn = re.compile(r'研究(?:方向|领域|重点)(?:为|包括)*[:：\n]*([ \n\S]+?[。\n])')
pos_ptn = re.compile(r"(教授|副教授|助理教授|研究员|副研究员|博士导师|硕士导师|博士生研究生|硕士生导师|IEEE Fellow|IEEE\s*会员|博士|讲师|教师|高级工程师|工程师)")
edu_ptn = re.compile(r"(?:考入|攻读|进修|毕业于|学位|博士后|访问|工程师)")
split_ptn = re.compile(r'([;；。\n])')


def teacher_list():
    urls = ["https://est.tongji.edu.cn/js/list.htm", "https://est.tongji.edu.cn/fjs/list.htm", "https://est.tongji.edu.cn/js_16255/list.htm", "https://est.tongji.edu.cn/zljs/list.htm"]

    for _url in urls:
        try:
            r = session.get(_url)
            if r.status_code == 200:
                lis = r.html.xpath("//ul[@class='pic-list'][1]/li/p[1]/a[1]")
                teachers = []

                for li in lis:
                    name = li.full_text.strip()
                    url = list(li.absolute_links)[0]

                    logging.info("name: %s, url: %s", name, url)
                    teachers.append((name, url))
                pool.imap_unordered(teacher_detail, teachers)
            else:
                logging.warning("wrong status code: %s", r.status_code)
                r.raise_for_status()
        except Exception as e:
            traceback.print_exc()
            raise e


def teacher_detail(payload):
    name, url = payload
    logging.info("loading %s ...", name)

    try:
        r = session.get(url)
        if r.status_code == 200:
            avatar = r.html.xpath("//div[@class='wp_articlecontent'][1]//img[1]/@src")
            avatar = r.html.base_url + avatar[0] if avatar else ''

            content = "\n".join([p.full_text.strip() for p in r.html.xpath("//div[@class='wp_articlecontent'][1]//p")])

            tel, email, addr, subj, ri = [ptn.search(content) for ptn in (tel_ptn, email_ptn, addr_ptn, subj_ptn, ri_ptn)]
            tel, email, addr, subj, ri = [(match.group(1).strip() if match else '') for match in (tel, email, addr, subj, ri)]

            pos = set(pos_ptn.findall(content))
            if '教授' in pos:
                pos.discard('助理教授')
                pos.discard('副教授')
                pos.discard('研究员')
                pos.discard('副研究员')
                pos.discard('讲师')
                pos.discard('教师')
            if '副教授' in pos:
                pos.discard('研究员')
                pos.discard('副研究员')
                pos.discard('讲师')
                pos.discard('教师')
            if '研究员' in pos:
                pos.discard('副研究员')

            pos = ','.join(list(pos))

            sentences = split_ptn.split(content)

            edu_sent = []
            for sent in sentences:
                if edu_ptn.search(sent):
                    edu_sent.append(sent)
            edu = "。".join(edu_sent)

            logging.info("name: %s, pos: %s, email: %s, tel: %s, addr: %s, subj: %s, edu: %s, ri: %s, avatar: %s", name, pos, email, tel, addr, subj, edu, ri, avatar)
            pass
        else:
            logging.warning("status code error: %s", r.status_code)
            r.raise_for_status()
    except Exception as e:
        traceback.print_exc()
        raise e


if __name__ == '__main__':
    teacher_list()

    pool.close()
    pool.join()

    session.close()

    pass

